package org.csu.mypetstoreapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.csu.mypetstoreapi.common.CommonResponse;
import org.csu.mypetstoreapi.entity.Account;
import org.csu.mypetstoreapi.entity.Profile;
import org.csu.mypetstoreapi.entity.SignOn;
import org.csu.mypetstoreapi.persistence.AccountMapper;
import org.csu.mypetstoreapi.persistence.ProfileMapper;
import org.csu.mypetstoreapi.persistence.SignOnMapper;
import org.csu.mypetstoreapi.service.AccountService;
import org.csu.mypetstoreapi.vo.AccountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private SignOnMapper signOnMapper;
    @Autowired
    private ProfileMapper profileMapper;
    @Override
    public CommonResponse<AccountVO> getAccount(String username,String password) {
        // 构造查询器先去signon表中查
        QueryWrapper<SignOn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password); // 明文密码
        SignOn signOn = signOnMapper.selectOne(queryWrapper);

        if (signOn==null) {
            return CommonResponse.createForError("用户名或者密码不正确");
        }
        Account account=accountMapper.selectById(username);
        AccountVO accountVO=accountToAccountVO(account);
        return getAccount(username);
    }

    @Override
    public CommonResponse<AccountVO> getAccount(String username) {
        Account account = accountMapper.selectById(username);
        Profile profile = profileMapper.selectById(username);
        SignOn signOn = signOnMapper.selectById(username);
        account.setLangpref(profile.getLangpref());
        account.setFavcategory(profile.getFavcategory());
        account.setPassword(signOn.getPassword());
        account.setMylistopt(profile.getMylistopt());
        account.setBanneropt(profile.getBanneropt());

        return CommonResponse.createForSuccess(accountToAccountVO(account));
    }

    @Override
    public CommonResponse<AccountVO> insertAccount(AccountVO accountVO) {
//        //对密码进行加密
//        account.setPassword(DigestUtils.md5Hex(account.getPassword()));
        //在Account表中添加用户信息
        Map<String, Object> datas = this.accountVOToPO(accountVO);
       Account account=(Account) datas.get("account");
       Profile profile=(Profile)datas.get("profile");

        //同时在signon表中添加用户信息
        SignOn signOn = (SignOn) datas.get("signon");
        accountMapper.insert(account);
        profileMapper.insert(profile);
        signOnMapper.insert(signOn);
        return CommonResponse.createForSuccess("成功创建用户！", accountVO);
    }

    @Override
    public CommonResponse<List<AccountVO>> findAllAccounts() {
        CommonResponse<List<AccountVO>> response;

        try {
            List<Account> accountList = accountMapper.selectList(null); // 假设使用accountService进行账户查询
            List<AccountVO> accountVOList = new ArrayList<>();

            for (Account account : accountList) {
                AccountVO accountVO = accountToAccountVO(account);
                accountVOList.add(accountVO);
            }
            response = CommonResponse.createForSuccess(accountVOList);
        } catch (Exception e) {
            response = CommonResponse.createForError("Failed to retrieve accounts: " + e.getMessage());
        }

        return response;
    }
    private AccountVO accountToAccountVO(Account account) {
        AccountVO accountVO = new AccountVO();
        accountVO.setUsername(account.getUsername());
        accountVO.setEmail(account.getEmail());
        accountVO.setFirstName(account.getFirstName());
        accountVO.setLastName(account.getLastName());
        accountVO.setStatus(account.getStatus());
        accountVO.setAddress1(account.getAddress1());
        accountVO.setAddress2(account.getAddress2());
        accountVO.setCity(account.getCity());
        accountVO.setState(account.getState());
        accountVO.setZip(account.getZip());
        accountVO.setCountry(account.getCountry());
        accountVO.setPhone(account.getPhone());
        accountVO.setLangpref(account.getLangpref());
        accountVO.setFavcategory(account.getFavcategory());
        accountVO.setMylistopt(account.getMylistopt());
        accountVO.setBanneropt(account.getBanneropt());
        accountVO.setPassword(account.getPassword());

        return accountVO;
    }

    // AccountVO拆分为Account、Profile、SignOn
    private Map<String, Object> accountVOToPO(AccountVO accountVO) {
        Account account = new Account();
        account.setUsername(accountVO.getUsername());
        account.setEmail(accountVO.getEmail());
        account.setFirstName(accountVO.getFirstName());
        account.setLastName(accountVO.getLastName());
        account.setStatus(accountVO.getStatus());
        account.setAddress1(accountVO.getAddress1());
        account.setAddress2(accountVO.getAddress2());
        account.setCity(accountVO.getCity());
        account.setState(accountVO.getState());
        account.setZip(accountVO.getZip());
        account.setCountry(accountVO.getCountry());
        account.setPhone(accountVO.getPhone());

        Profile profile = new Profile();


        SignOn signOn = new SignOn();
        signOn.setUsername(accountVO.getUsername());
        signOn.setPassword(accountVO.getPassword());

        Map<String, Object> res = new HashMap<>();
        res.put("account", account);
        res.put("profile", profile);
        res.put("signon", signOn);
        return res;
    }
    public Account accountVOToAccount(AccountVO accountVO) {
        Account account = new Account();
        account.setUsername(accountVO.getUsername());
        account.setEmail(accountVO.getEmail());
        account.setFirstName(accountVO.getFirstName());
        account.setLastName(accountVO.getLastName());
        account.setStatus(accountVO.getStatus());
        account.setAddress1(accountVO.getAddress1());
        account.setAddress2(accountVO.getAddress2());
        account.setCity(accountVO.getCity());
        account.setState(accountVO.getState());
        account.setZip(accountVO.getZip());
        account.setCountry(accountVO.getCountry());
        account.setPhone(accountVO.getPhone());

        account.setLangpref(accountVO.getLangpref());
        account.setFavcategory(accountVO.getFavcategory());
        account.setMylistopt(accountVO.getMylistopt());
        account.setBanneropt(accountVO.getBanneropt());

        return account;
    }

    @Override
    public Account getAccountByUsername(String username) {
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq("userid",username);
        return accountMapper.selectOne(accountQueryWrapper);
    }
}


